{% extends "templates/atnet_base.html" %}

{% block csslink %}
		{{ super() }}
<script src="/media/js/jquery.js"></script>

{% include 'templates/includes_datatable.html' %}
{% include 'templates/includes_jquery_ui.html' %}
<script src="/media/js/form_popup.js"></script>

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
	var wasAction = false;
	var obj_id = 0;
	
	$('#suppliers').dataTable({
		"bPaginate": false,
		"bLengthChange": false,
		"bFilter": true,
		"bSort": true,
		"bInfo": false,
		"bAutoWidth": false,
		"oLanguage": {
            "sUrl": "/media/js/dataTables.{{ app_user.lang }}.txt"
        },
		"bProcessing": true,
		"sAjaxSource": "{{ uri_for('vie_suppliers', imm_id=immeuble.key().id(), command='list_json') }}",
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
			$('td:eq(7)', nRow).html('<a href="javascript:void(0);" id="' + aData[7] + '">{{ _("Edit") }}</a>');
		},
	});
	
	$("#generated-form").ajaxComplete(function() {
		$(this).find('#contact').parent().append("<br />");
		$(this).find('#contact').parent().append("<a href='javascript:void(0);' id='contact-add-person'>Add person</a>");
		$(this).find('#contact').parent().append("<br />");
		$(this).find('#contact').parent().append("<a href='javascript:void(0);' id='contact-add-company'>Add company</a>");
		
		$(this).find('#type_of_work').parent().append("<br />");
		$(this).find('#type_of_work').parent().append("<a href='javascript:void(0);' id='type-of-work-add'>Add type of work</a>");
	});
	
	$('#contact-add-person').live('click', function(){
		$("#dialog-form").dialog("close"); // or hide to keep already entered data
		$(this).LaunchPopup(
			{
				DivId: '#dialog-form1',
				FormId: '#generated-form1',
				UrlForm: "{{ uri_for('vie_contacts', imm_id=immeuble.key().id(), command='add_person') }}",
				Mode: 'add',
				Title: '{{ _("Add person") }}',
				ControlId: '#add-ba'
			}
		)
	});
	
	$('#contact-add-company').live('click', function(){
		$("#dialog-form").dialog("close");
		$(this).LaunchPopup(
			{
				DivId: '#dialog-form1',
				FormId: '#generated-form1',
				UrlForm: "{{ uri_for('vie_contacts', imm_id=immeuble.key().id(), command='add_company') }}",
				Mode: 'add',
				Title: '{{ _("Add company") }}',
				ControlId: '#add-ba'
			}
		)
	});
	
	$('#type-of-work-add').live('click', function(){
		$("#dialog-form").dialog("close"); // or hide to keep already entered data
		$(this).LaunchPopup(
			{
				DivId: '#dialog-form1',
				FormId: '#generated-form1',
				UrlForm: "{{ uri_for('vie_work_types', imm_id=immeuble.key().id(), command='add') }}",
				Mode: 'add',
				Title: '{{ _("Add type of work") }}',
				ControlId: '#add-ba'
			}
		)
	});
	
	$('#add-person').live('click', function(){
		obj_id = 0;
		$(this).CallAddPopup1();
	});
	
	$('#add-company').live('click', function(){
		obj_id = 0;
		$(this).CallAddPopup2();
	});
	
	$('#suppliers tbody tr a').live('click', function(e) {
		obj_id = e.target.id;
		$(this).CallEditPopup();
	});

	$.fn.SecondPopupClose = function() {
		if (obj_id == 0)
			$(this).CallAddPopup();
		else
			$(this).CallEditPopup(obj_id);
	};
	
	$.fn.CallAddPopup1 = function() {
		$('#generated-form').html('');
		$(this).LaunchPopup(
				{
					TableId: '#suppliers',
					DivId: '#dialog-form',
					FormId: '#generated-form',
					UrlForm: "{{ uri_for('vie_suppliers', imm_id=immeuble.key().id(), command='add') }}",
					Mode: 'add',
					Title: '{{ _("Add supplier (person)") }}'
				}
			)
	};

	$.fn.CallAddPopup2 = function() {
		$('#generated-form').html('');
		$(this).LaunchPopup(
				{
					TableId: '#suppliers',
					DivId: '#dialog-form',
					FormId: '#generated-form',
					UrlForm: "{{ uri_for('vie_suppliers', imm_id=immeuble.key().id(), command='add_company') }}",
					Mode: 'add',
					Title: '{{ _("Add supplier (company)") }}'
				}
			)
	};
	
	$.fn.CallEditPopup = function() {
		$('#generated-form').html('');
		$(this).LaunchPopup(
				{
					TableId: '#suppliers',
					DivId: '#dialog-form',
					FormId: '#generated-form',
					UrlForm: "{{ uri_for('vie_suppliers', imm_id=immeuble.key().id(), command='edit') }}" + obj_id + "/",
					Mode: 'edit',
					Title: '{{ _("Edit suppplier") }}'
				}
			)
	};
	
});
</script>
{% endblock %}

{% block title %} 
	{{ title }}
{% endblock %}

{% block header %}
{{ header }}
{% endblock %}

{% block login%}
{% if show_login %}
{{ super() }}
{% endif %}
{% endblock%}

{% block navigation %}
{% if not show_login %}
{{ super() }}
{% endif %}
{% endblock %}

{% block section_menu %}
{% include 'templates/includes_vie_de_la_copropriete.html' %}
{% endblock %}

{% block main_content %}
<p>
	<p><a href="javascript:void(0);" id="add-person">{{ _('Add supplier (person)') }}</a> || 
	<a href="javascript:void(0);" id="add-company">{{ _('Add supplier (company)') }}</a></p>
	<div id="demo">
	<table cellpadding="0" cellspacing="0" border="0" class="display" id="suppliers" width="100%">
		<thead>
			<tr>
				<th>{{ _(fields.name) }}</th>
				<th>{{ _(fields.type_of_work) }}</th>
				<th>{{ _(fields.tel) }}</th>
				<th>{{ _(fields.fax) }}</th>
				<th>{{ _(fields.address) }}</th>
				<th>{{ _(fields.contact_person) }}</th>
				<th>{{ _(fields.email) }}</th>
				<th width="20"></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td colspan="11" class="dataTables_empty">{{ _('Loading data from server') }}</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<th>{{ _(fields.name) }}</th>
				<th>{{ _(fields.type_of_work) }}</th>
				<th>{{ _(fields.tel) }}</th>
				<th>{{ _(fields.fax) }}</th>
				<th>{{ _(fields.address) }}</th>
				<th>{{ _(fields.contact_person) }}</th>
				<th>{{ _(fields.email) }}</th>
				<th></th>
			</tr>
		</tfoot>
	</table>
	</div>
	{% include 'templates/includes_popup_form.html' %}
	{% include 'templates/includes_popup_form1.html' %}
</p>
<script>
$(function() {
	var user_lang = "{{ app_user.lang }}";
	if (user_lang == "en") { user_lang = "en-GB" };
	$.datepicker.setDefaults( $.datepicker.regional[ user_lang ] );
	
	$("#birthday").live("focus", function() {
	    $(this).datepicker({
	        inline: true 
	    });
	});

});
</script>

{% endblock %}
